package com.example.zhyq.park

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.ParamPark
import com.example.base.mvvm.BaseActivity
import com.example.zhyq.R
import com.example.zhyq.databinding.ActivityAddParkBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 * 添加车位
 */
@Route(path = "/app/AddParkActivity")
@AndroidEntryPoint
class AddParkActivity : BaseActivity<ActivityAddParkBinding, ParkViewModel>() {

    var area = ""
    var type = ""
    var status = ""
    override fun initView() {
        binding.headPark.headerBack.setOnClickListener {
            finish()
        }
        binding.headPark.headerTitle.setText("添加车位")
        binding.headPark.headerAdd.setImageResource(R.drawable.baseline_check_24)

        //所选区域
        val array = arrayOf("A区停车场", "B区停车场", "C区停车场")
        binding.spArea.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, array)
        binding.spArea.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
               area=array[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

        //车位类型
        val arrayOf = arrayOf("标准车位", "VIP车位")
        val adapter =
            ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, arrayOf)
        binding.spType.adapter = adapter
        binding.spType.onItemSelectedListener=object :OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                type=arrayOf[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

        //备注信息
        binding.etBeizhu.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                val length = p0?.length
                binding.tvNum.text = "$length/10"
                if (length==10) {
                    binding.tvNum.setTextColor(Color.RED)
                }else {
                    binding.tvNum.setTextColor(Color.GRAY)
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }

        })

        binding.drawRg.setOnCheckedChangeListener { radioGroup, i ->
            if (i==R.id.rb_kx) {
                status = "空闲"
            }else if (i==R.id.rb_zy) {
                status = "已占用"
            }else {
                status = "维修中"
            }
        }

        //添加车位
        binding.btnAdd.setOnClickListener {
            val carNo = binding.etCarNo.text.toString()
            val name = binding.etName.text.toString()
            val licence = binding.etLicense.text.toString()
            val beizhu = binding.etBeizhu.text.toString()
            val paramPark = ParamPark()
            paramPark.areaName = area
            paramPark.spaceType = type
            paramPark.spaceNumber = carNo
            paramPark.userId = 890
            paramPark.userName = name
            paramPark.licensePlate = licence
            paramPark.areaId = 3
            paramPark.remarks = beizhu
            paramPark.spaceStatus =status

            vm.addPark(paramPark)
        }
    }

    override fun initData() {
        vm.addParkSuc.observe(this) {
            ToastUtils.showLong("添加成功")
            finish()
        }
    }
}